package smartbooks.ui.customers;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import smartbooks.exceptions.MyException;
import smartbooks.restrictDocuments.JTextFieldMaxLimitDocument;
import smartbooks.ui.SmartBooksUserSettings;
import smartbooks.ui.listeners.CommonMouseListener;
import smartbooks.ui.manager.CustomerManager;
import smartbooks.ui.util.CommonConstants;
import smartbooks.ui.util.CommonFunctions;
import smartbooksdb.exception.MandatoryFieldException;
import smartbooksdb.hibernatebean.Customer;

/**
 *
 * @author Ankit
 */
public class CustomersPanel extends javax.swing.JPanel implements CommonConstants {
    private ArrayList states = new ArrayList();
    private Customer editCustomer = null;
    Customer customer = new Customer();
    
    /**
     * Creates new form CustomersPanel
     * @param editCustomer
     */
    public CustomersPanel(Customer editCustomer) {
        setAllStates();
        
        // We set this in item object initially so that we can set the default here first then it can be modified
        customer.setBillingEqualsShipping(true);
        
        if(null!=editCustomer) {
            this.editCustomer = editCustomer;
            this.customer = editCustomer;
        }
        
        customer.setBillToAddress((null==customer.getBillToAddress()) ? DEFAULT_BILLTO_ADDRESS : customer.getBillToAddress());
        customer.setBillToState((null==customer.getBillToState()) ? DEFAULT_BILLTO_STATE : customer.getBillToState());
        customer.setBillToCity((null==customer.getBillToCity()) ? DEFAULT_BILLTO_CITY : customer.getBillToCity());
        customer.setCustomerTax((null==customer.getCustomerTax()) ? DEFAULT_CUSTOMER_TAX : customer.getCustomerTax());
        customer.setGender((null==customer.getGender()) ? GENDER_MALE : customer.getGender());
        
        initComponents();
        
        customInitComponents();
    }

    private void setAllStates() {
        this.states.addAll(stateCityMapping.keySet());
    }
    
    private void customInitComponents() {
        addKeyListenersToComponents(keyListener);
        
        if(customer.getCustomerId()==0) {
            customerPanel.setBorder(new TitledBorder(customerPanel.getBorder(), CUSTOMER_PANEL_CREATE_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveCustomerButton.setText(CREATE_BUTTON_TEXT);
            
            setShipToAddressAsBillToAddress();
        } else {
            customerPanel.setBorder(new TitledBorder(customerPanel.getBorder(), CUSTOMER_PANEL_EDIT_TEXT, 2, 0, new Font("Default", 0, 14)));
            createOrSaveCustomerButton.setText(SAVE_BUTTON_TEXT);
            
            if(!billToAddressTextArea.getText().equals("") && !billToAddressTextArea.getText().equals(DEFAULT_BILLTO_ADDRESS)) {
                billToAddressTextArea.setForeground(Color.BLACK);
            }
            if(!shipToAddressTextArea.getText().equals("") && !shipToAddressTextArea.getText().equals(DEFAULT_SHIPTO_ADDRESS)) {
                shipToAddressTextArea.setForeground(Color.BLACK);
            }
            billingEqualsShippingCheckBoxStateChanged(null);
            genderButtonGroup.setSelected((customer.getGender().equals(GENDER_MALE) ? maleRadioButton.getModel() : femaleRadioButton.getModel()), true);
            dOBDateChooser.setDate(customer.getDob());
        }
    }
    
    /**
     * This method is used to get States
     * @return states
     */
    public ArrayList getStates() {
        return this.states;
    }
    
    /**
     * This method is used to set States
     * @param states
     */
    public void setStates(ArrayList states) {
        this.states = states;
    }
    
    /**
     * This method is used to get Customer
     * @return customer
     */
    public Customer getCustomer() {
        return this.customer;
    }
    
    /**
     * This method is used to set Customer
     * @param customer
     */
    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
    
    private void setShipToAddressAsBillToAddress() {
        shipToAddressTextArea.setText(billToAddressTextArea.getText().equals(DEFAULT_BILLTO_ADDRESS) ? "" : billToAddressTextArea.getText());
        shipToStateComboBox.setSelectedItem(billToStateComboBox.getSelectedItem());
        shipToCityComboBox.setSelectedItem(billToCityComboBox.getSelectedItem());
        
        shipToAddressTextArea.setForeground(Color.GRAY);
    }
    
    private void addKeyListenersToComponents(KeyListener keyListener) {
        customerNameTextField.addKeyListener(keyListener);
        contactPersonTextField.addKeyListener(keyListener);
        billToStateComboBox.addKeyListener(keyListener);
        billToCityComboBox.addKeyListener(keyListener);
        shipToStateComboBox.addKeyListener(keyListener);
        shipToCityComboBox.addKeyListener(keyListener);
        billingEqualsShippingCheckBox.addKeyListener(keyListener);
        maleRadioButton.addKeyListener(keyListener);
        femaleRadioButton.addKeyListener(keyListener);
        dOBDateChooser.getComponent(1).addKeyListener(keyListener);
        emailTextField.addKeyListener(keyListener);
        phoneTextField.addKeyListener(keyListener);
        mobileTextField.addKeyListener(keyListener);
        officeTextField.addKeyListener(keyListener);
        faxTextField.addKeyListener(keyListener);
        customerTaxComboBox.addKeyListener(keyListener);
        additionalInfoTextField.addKeyListener(keyListener);
    }
    
    private void assignAdditionalCustomerValues(String customerModificationType) {
        if(null!=customer.getBillToAddress() && customer.getBillToAddress().equals(DEFAULT_BILLTO_ADDRESS)) {
            customer.setBillToAddress(null);
        }
        if(null!=customer.getBillToState() && customer.getBillToState().equals(DEFAULT_BILLTO_STATE)) {
            customer.setBillToState(null);
        }
        if(null!=customer.getBillToCity() && customer.getBillToCity().equals(DEFAULT_BILLTO_CITY)) {
            customer.setBillToCity(null);
        }
        
        if(null!=customer.getShipToAddress() && customer.getShipToAddress().equals(DEFAULT_SHIPTO_ADDRESS)) {
            customer.setShipToAddress(null);
        }
        if(null!=customer.getShipToState() && customer.getShipToState().equals(DEFAULT_SHIPTO_STATE)) {
            customer.setShipToState(null);
        }
        if(null!=customer.getShipToCity() && customer.getShipToCity().equals(DEFAULT_SHIPTO_CITY)) {
            customer.setShipToCity(null);
        }
        
        customer.setGender((genderButtonGroup.getSelection().equals(maleRadioButton.getModel())) ? GENDER_MALE : GENDER_FEMALE);
        customer.setDob(dOBDateChooser.getDate());
        if(customerModificationType.equals(CREATE_BUTTON_TEXT)) {
            customer.setCreatedBy(SmartBooksUserSettings.USERNAME);
        }
        customer.setUpdatedBy(SmartBooksUserSettings.USERNAME);
    }
    
    private void setModifiedCustomerInParent() {
        Container c = this;
        
        while(c.getParent()!=null) {
            c = c.getParent();
            
            if(c instanceof CustomersDialog) {
                ((CustomersDialog)c).setModifiedCustomer(customer);
            }
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        genderButtonGroup = new javax.swing.ButtonGroup();
        customerPanel = new javax.swing.JPanel();
        customerNameLabel = new javax.swing.JLabel();
        customerNameTextField = new javax.swing.JTextField();
        contactPersonLabel = new javax.swing.JLabel();
        contactPersonTextField = new javax.swing.JTextField();
        addressTabbedPane = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        billToAddressTextArea = new javax.swing.JTextArea();
        billToStateComboBox = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        billToCityComboBox = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        shipToAddressTextArea = new javax.swing.JTextArea();
        shipToStateComboBox = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        shipToCityComboBox = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        billingEqualsShippingCheckBox = new javax.swing.JCheckBox();
        genderLabel = new javax.swing.JLabel();
        maleRadioButton = new javax.swing.JRadioButton();
        femaleRadioButton = new javax.swing.JRadioButton();
        jLabel12 = new javax.swing.JLabel();
        dOBDateChooser = new com.toedter.calendar.JDateChooser("dd/MM/yyyy","##/##/####",' ');
        jSeparator1 = new javax.swing.JSeparator();
        jLabel13 = new javax.swing.JLabel();
        emailTextField = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        phoneTextField = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        mobileTextField = new javax.swing.JTextField();
        jLabel16 = new javax.swing.JLabel();
        officeTextField = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        faxTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        customerTaxComboBox = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        additionalInfoTextField = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        internalNotesTextArea = new javax.swing.JTextArea();
        jSeparator2 = new javax.swing.JSeparator();
        createOrSaveCustomerButton = new javax.swing.JButton();
        clearCustomerButton = new javax.swing.JButton();

        customerPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " ", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14))); // NOI18N

        customerNameLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        customerNameLabel.setText("<html><font color=red>*</font>Customer Name:</html>");

        customerNameTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        customerNameTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.customerName}"), customerNameTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "customerNameBinding");
        bindingGroup.addBinding(binding);

        contactPersonLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        contactPersonLabel.setText("Contact Person:");

        contactPersonTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        contactPersonTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.contactPerson}"), contactPersonTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "contactPersonBinding");
        bindingGroup.addBinding(binding);

        addressTabbedPane.setFocusable(false);
        addressTabbedPane.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jPanel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jScrollPane2.setBorder(null);
        jScrollPane2.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        billToAddressTextArea.setColumns(20);
        billToAddressTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        billToAddressTextArea.setForeground(new java.awt.Color(128, 128, 128));
        billToAddressTextArea.setLineWrap(true);
        billToAddressTextArea.setRows(4);
        billToAddressTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.billToAddress}"), billToAddressTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "billToAddressBinding");
        bindingGroup.addBinding(binding);

        billToAddressTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                billToAddressTextAreaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                billToAddressTextAreaFocusLost(evt);
            }
        });
        billToAddressTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                billToAddressTextAreaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                billToAddressTextAreaKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(billToAddressTextArea);

        billToStateComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${states}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, billToStateComboBox, "billToAllStatesBinding");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.billToState}"), billToStateComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "billToStateBinding");
        bindingGroup.addBinding(binding);

        billToStateComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                billToStateComboBoxItemStateChanged(evt);
            }
        });

        jLabel9.setText("State");
        jLabel9.setEnabled(false);

        billToCityComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.billToCity}"), billToCityComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "billToCityBinding");
        bindingGroup.addBinding(binding);

        billToCityComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                billToCityComboBoxItemStateChanged(evt);
            }
        });

        jLabel8.setText("City");
        jLabel8.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(billToStateComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(billToCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(billToStateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(billToCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addressTabbedPane.addTab("Bill To Address", jPanel2);

        jScrollPane3.setBorder(null);
        jScrollPane3.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        shipToAddressTextArea.setColumns(20);
        shipToAddressTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        shipToAddressTextArea.setLineWrap(true);
        shipToAddressTextArea.setRows(4);
        shipToAddressTextArea.setWrapStyleWord(true);
        shipToAddressTextArea.setEnabled(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.shipToAddress}"), shipToAddressTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "shipToAddressBinding");
        bindingGroup.addBinding(binding);

        shipToAddressTextArea.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                shipToAddressTextAreaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                shipToAddressTextAreaFocusLost(evt);
            }
        });
        shipToAddressTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                shipToAddressTextAreaKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(shipToAddressTextArea);

        shipToStateComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        shipToStateComboBox.setEnabled(false);

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${states}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, shipToStateComboBox, "shipToAllStatesBinding");
        bindingGroup.addBinding(jComboBoxBinding);
        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.shipToState}"), shipToStateComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "shipToStateBinding");
        bindingGroup.addBinding(binding);

        shipToStateComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                shipToStateComboBoxItemStateChanged(evt);
            }
        });

        jLabel10.setText("State");
        jLabel10.setEnabled(false);

        shipToCityComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        shipToCityComboBox.setEnabled(false);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.shipToCity}"), shipToCityComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "shipToCityBinding");
        bindingGroup.addBinding(binding);

        jLabel19.setText("City");
        jLabel19.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(shipToStateComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(shipToCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(shipToStateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(shipToCityComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel19))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addressTabbedPane.addTab("Ship To Address", jPanel3);

        billingEqualsShippingCheckBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        billingEqualsShippingCheckBox.setText("Use 'Bill To' address as 'Ship To' address");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.billingEqualsShipping}"), billingEqualsShippingCheckBox, org.jdesktop.beansbinding.BeanProperty.create("selected"), "billingEqualsShippingBinding");
        bindingGroup.addBinding(binding);

        billingEqualsShippingCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                billingEqualsShippingCheckBoxStateChanged(evt);
            }
        });

        genderLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        genderLabel.setText("Gender:");

        genderButtonGroup.add(maleRadioButton);
        maleRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        maleRadioButton.setSelected(true);
        maleRadioButton.setText("Male");

        genderButtonGroup.add(femaleRadioButton);
        femaleRadioButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        femaleRadioButton.setText("Female");

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel12.setText("DOB:");

        dOBDateChooser.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        dOBDateChooser.setMaxSelectableDate(new Date());

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel13.setText("Email:");

        emailTextField.setDocument(new JTextFieldMaxLimitDocument(64));
        emailTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.email}"), emailTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "emailBinding");
        bindingGroup.addBinding(binding);

        jLabel14.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel14.setText("Phone:");

        phoneTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        phoneTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.phone}"), phoneTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "phoneBinding");
        bindingGroup.addBinding(binding);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel15.setText("Mobile:");

        mobileTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        mobileTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.mobile}"), mobileTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "mobileBinding");
        bindingGroup.addBinding(binding);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel16.setText("Office:");

        officeTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        officeTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.office}"), officeTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "officeBinding");
        bindingGroup.addBinding(binding);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel17.setText("Fax:");

        faxTextField.setDocument(new JTextFieldMaxLimitDocument(16));
        faxTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.fax}"), faxTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "faxBinding");
        bindingGroup.addBinding(binding);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Customer Tax:");

        customerTaxComboBox.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        customerTaxComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Default", "Exempt" }));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.customerTax}"), customerTaxComboBox, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"), "customerTaxBinding");
        bindingGroup.addBinding(binding);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setText("Additional Info:");

        additionalInfoTextField.setDocument(new JTextFieldMaxLimitDocument(128));
        additionalInfoTextField.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.additionalInfo}"), additionalInfoTextField, org.jdesktop.beansbinding.BeanProperty.create("text"), "additionalInfoBinding");
        bindingGroup.addBinding(binding);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel18.setText("Internal Notes:");

        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        internalNotesTextArea.setColumns(20);
        internalNotesTextArea.setDocument(new JTextFieldMaxLimitDocument(256));
        internalNotesTextArea.setLineWrap(true);
        internalNotesTextArea.setRows(3);
        internalNotesTextArea.setWrapStyleWord(true);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${customer.notes}"), internalNotesTextArea, org.jdesktop.beansbinding.BeanProperty.create("text"), "notesBinding");
        bindingGroup.addBinding(binding);

        internalNotesTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                internalNotesTextAreaKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(internalNotesTextArea);

        createOrSaveCustomerButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        createOrSaveCustomerButton.setText("Create");
        createOrSaveCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createOrSaveCustomerButtonActionPerformed(evt);
            }
        });

        clearCustomerButton.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        clearCustomerButton.setText("Clear");
        clearCustomerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearCustomerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout customerPanelLayout = new javax.swing.GroupLayout(customerPanel);
        customerPanel.setLayout(customerPanelLayout);
        customerPanelLayout.setHorizontalGroup(
            customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(customerPanelLayout.createSequentialGroup()
                            .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel16, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(customerPanelLayout.createSequentialGroup()
                                    .addComponent(jLabel15)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(mobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(customerPanelLayout.createSequentialGroup()
                                    .addComponent(jLabel17)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(faxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addComponent(jLabel18)
                        .addGroup(customerPanelLayout.createSequentialGroup()
                            .addComponent(jLabel6)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(additionalInfoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(customerPanelLayout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(customerTaxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(billingEqualsShippingCheckBox)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(customerPanelLayout.createSequentialGroup()
                            .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(customerNameLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(contactPersonLabel, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(contactPersonTextField)
                                .addComponent(customerNameTextField)))
                        .addComponent(addressTabbedPane)
                        .addGroup(customerPanelLayout.createSequentialGroup()
                            .addComponent(genderLabel)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(maleRadioButton)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(femaleRadioButton)
                            .addGap(75, 75, 75)
                            .addComponent(jLabel12)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(dOBDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(customerPanelLayout.createSequentialGroup()
                        .addGap(49, 49, 49)
                        .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, customerPanelLayout.createSequentialGroup()
                                .addComponent(clearCustomerButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createOrSaveCustomerButton))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, customerPanelLayout.createSequentialGroup()
                                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(phoneTextField)
                                    .addComponent(officeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(211, 211, 211)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        customerPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clearCustomerButton, createOrSaveCustomerButton});

        customerPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {faxTextField, mobileTextField, officeTextField, phoneTextField});

        customerPanelLayout.setVerticalGroup(
            customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(customerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(customerNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(customerNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(contactPersonLabel)
                    .addComponent(contactPersonTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(addressTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(billingEqualsShippingCheckBox)
                .addGap(18, 18, 18)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(genderLabel)
                    .addComponent(maleRadioButton)
                    .addComponent(femaleRadioButton)
                    .addComponent(jLabel12)
                    .addComponent(dOBDateChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(emailTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel14)
                    .addComponent(phoneTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15)
                    .addComponent(mobileTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel16)
                    .addComponent(officeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(faxTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(customerTaxComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(additionalInfoTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(customerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(createOrSaveCustomerButton)
                    .addComponent(clearCustomerButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(customerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(customerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();
    }// </editor-fold>//GEN-END:initComponents

    private void billToAddressTextAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_billToAddressTextAreaFocusGained
        if(billToAddressTextArea.getText().equals(DEFAULT_BILLTO_ADDRESS)) {
            billToAddressTextArea.setText("");
        }
        billToAddressTextArea.setForeground(Color.BLACK);
    }//GEN-LAST:event_billToAddressTextAreaFocusGained

    private void billToAddressTextAreaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_billToAddressTextAreaFocusLost
        if(billToAddressTextArea.getText().equals("")) {
            billToAddressTextArea.setText(DEFAULT_BILLTO_ADDRESS);
            billToAddressTextArea.setForeground(Color.GRAY);
        }
    }//GEN-LAST:event_billToAddressTextAreaFocusLost

    private void billToAddressTextAreaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_billToAddressTextAreaKeyReleased
        if(billingEqualsShippingCheckBox.isSelected()){
            shipToAddressTextArea.setText(billToAddressTextArea.getText());
        }
    }//GEN-LAST:event_billToAddressTextAreaKeyReleased

    private void shipToAddressTextAreaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_shipToAddressTextAreaFocusGained
        if(shipToAddressTextArea.getText().equals(DEFAULT_SHIPTO_ADDRESS)) {
            shipToAddressTextArea.setText("");
        }
        shipToAddressTextArea.setForeground(Color.BLACK);
    }//GEN-LAST:event_shipToAddressTextAreaFocusGained

    private void shipToAddressTextAreaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_shipToAddressTextAreaFocusLost
        if(shipToAddressTextArea.getText().equals("") || shipToAddressTextArea.getText().equals(DEFAULT_SHIPTO_ADDRESS)) {
            shipToAddressTextArea.setText(DEFAULT_SHIPTO_ADDRESS);
            shipToAddressTextArea.setForeground(Color.GRAY);
        } else {
            shipToAddressTextArea.setForeground(Color.BLACK);
        }
    }//GEN-LAST:event_shipToAddressTextAreaFocusLost

    private void billingEqualsShippingCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_billingEqualsShippingCheckBoxStateChanged
        if(billingEqualsShippingCheckBox.isSelected()) {
            setShipToAddressAsBillToAddress();
        } else {
            shipToAddressTextAreaFocusLost(null);
        }
        
        shipToAddressTextArea.setEnabled(!billingEqualsShippingCheckBox.isSelected());
        shipToStateComboBox.setEnabled(!billingEqualsShippingCheckBox.isSelected());
        shipToCityComboBox.setEnabled(!billingEqualsShippingCheckBox.isSelected());
    }//GEN-LAST:event_billingEqualsShippingCheckBoxStateChanged

    private void billToStateComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_billToStateComboBoxItemStateChanged
        String state = String.valueOf(billToStateComboBox.getSelectedItem());
        ArrayList cities = stateCityMapping.get(state);

        if(null==cities)
            billToCityComboBox.setModel(new DefaultComboBoxModel());
        else
            billToCityComboBox.setModel(new DefaultComboBoxModel(cities.toArray()));
        
        if(billingEqualsShippingCheckBox.isSelected()){
            shipToStateComboBox.setSelectedItem(billToStateComboBox.getSelectedItem());
            shipToCityComboBox.setSelectedItem(billToCityComboBox.getSelectedItem());
        }
    }//GEN-LAST:event_billToStateComboBoxItemStateChanged

    private void shipToStateComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_shipToStateComboBoxItemStateChanged
        String state = String.valueOf(shipToStateComboBox.getSelectedItem());
        ArrayList cities = stateCityMapping.get(state);

        if(null==cities)
            shipToCityComboBox.setModel(new DefaultComboBoxModel());
        else
            shipToCityComboBox.setModel(new DefaultComboBoxModel(cities.toArray()));
    }//GEN-LAST:event_shipToStateComboBoxItemStateChanged

    private void billToCityComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_billToCityComboBoxItemStateChanged
        if(billingEqualsShippingCheckBox.isSelected()){
            shipToStateComboBox.setSelectedItem(billToStateComboBox.getSelectedItem());
            shipToCityComboBox.setSelectedItem(billToCityComboBox.getSelectedItem());
        }
    }//GEN-LAST:event_billToCityComboBoxItemStateChanged

    private void billToAddressTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_billToAddressTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_billToAddressTextAreaKeyPressed

    private void shipToAddressTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_shipToAddressTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_shipToAddressTextAreaKeyPressed

    private void internalNotesTextAreaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_internalNotesTextAreaKeyPressed
        CommonFunctions.textboxTABPolicy(evt);
    }//GEN-LAST:event_internalNotesTextAreaKeyPressed

    private void clearCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearCustomerButtonActionPerformed
        customerNameTextField.setText("");
        contactPersonTextField.setText("");
        billToAddressTextArea.setText(DEFAULT_BILLTO_ADDRESS);
        billToAddressTextArea.setForeground(Color.GRAY);
        billToStateComboBox.setSelectedItem(DEFAULT_BILLTO_STATE);
        // Above LOC will automatically make CityComboBox selected index to '0'
        // because it calls item state changed of the combobox
        billingEqualsShippingCheckBox.setSelected(true);
        billingEqualsShippingCheckBoxStateChanged(null);
        maleRadioButton.setSelected(true);
        dOBDateChooser.setDate(null);
        emailTextField.setText("");
        phoneTextField.setText("");
        mobileTextField.setText("");
        officeTextField.setText("");
        faxTextField.setText("");
        customerTaxComboBox.setSelectedItem(DEFAULT_CUSTOMER_TAX);
        additionalInfoTextField.setText("");
        internalNotesTextArea.setText("");
    }//GEN-LAST:event_clearCustomerButtonActionPerformed

    private void createOrSaveCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createOrSaveCustomerButtonActionPerformed
        CommonMouseListener.hideException = false;
        
        try {
            if(null==dOBDateChooser.getDate() && !((JTextField)dOBDateChooser.getComponent(1)).getText().equals(DATE_PATTERN)) {
                MyException me = new MyException("", "Invalid Date of birth");
                me.showBalloonTip((JComponent)dOBDateChooser.getComponent(1));
                ((JComponent)dOBDateChooser.getComponent(1)).requestFocus();
                return;
            }

            assignAdditionalCustomerValues(createOrSaveCustomerButton.getText());
            
            boolean customerModified = false;
            switch (createOrSaveCustomerButton.getText()) {
                case CREATE_BUTTON_TEXT:
                    customerModified = CustomerManager.createCustomer(customer);
                    break;
                case SAVE_BUTTON_TEXT:
                    customerModified = CustomerManager.updateCustomer(customer, editCustomer);
                    break;
            }
            
            if(customerModified) {
                Toolkit.getDefaultToolkit().beep();
                
                switch (createOrSaveCustomerButton.getText()) {
                    case CREATE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Customer is created successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                    case SAVE_BUTTON_TEXT:
                        JOptionPane.showMessageDialog(this,"Customer is updated successfully !","SmartBooks - Success",JOptionPane.INFORMATION_MESSAGE);
                        break;
                }
                
                setModifiedCustomerInParent();
                CommonFunctions.dispose(this);
            } else {
                Toolkit.getDefaultToolkit().beep();
                JOptionPane.showMessageDialog(this,"There was some error in creating Customer.\nKindly contact system administrator.","SmartBooks - Error",JOptionPane.ERROR_MESSAGE);
            }
        } catch (MyException me) {
            if(me.getComponentText().equalsIgnoreCase(CUSTOMER_NAME)) {
                me.showBalloonTip(customerNameTextField);
                customerNameTextField.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(GENDER)) {
                me.showBalloonTip(genderLabel);
            } else if(me.getComponentText().equalsIgnoreCase(BILL_TO_STATE)) {
                me.showBalloonTip(billToStateComboBox);
                billToStateComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(BILL_TO_CITY)) {
                me.showBalloonTip(billToCityComboBox);
                billToCityComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(SHIP_TO_STATE)) {
                me.showBalloonTip(shipToStateComboBox);
                shipToStateComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(SHIP_TO_CITY)) {
                me.showBalloonTip(shipToCityComboBox);
                shipToCityComboBox.requestFocus();
            } else if(me.getComponentText().equalsIgnoreCase(EMAIL)) {
                me.showBalloonTip(emailTextField);
                emailTextField.requestFocus();
            }
        } catch (MandatoryFieldException ex) {
            Logger.getLogger(CustomersPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_createOrSaveCustomerButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField additionalInfoTextField;
    private javax.swing.JTabbedPane addressTabbedPane;
    private javax.swing.JTextArea billToAddressTextArea;
    private javax.swing.JComboBox billToCityComboBox;
    private javax.swing.JComboBox billToStateComboBox;
    private javax.swing.JCheckBox billingEqualsShippingCheckBox;
    private javax.swing.JButton clearCustomerButton;
    private javax.swing.JLabel contactPersonLabel;
    private javax.swing.JTextField contactPersonTextField;
    private javax.swing.JButton createOrSaveCustomerButton;
    private javax.swing.JLabel customerNameLabel;
    private javax.swing.JTextField customerNameTextField;
    private javax.swing.JPanel customerPanel;
    private javax.swing.JComboBox customerTaxComboBox;
    private com.toedter.calendar.JDateChooser dOBDateChooser;
    private javax.swing.JTextField emailTextField;
    private javax.swing.JTextField faxTextField;
    private javax.swing.JRadioButton femaleRadioButton;
    private javax.swing.ButtonGroup genderButtonGroup;
    private javax.swing.JLabel genderLabel;
    private javax.swing.JTextArea internalNotesTextArea;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JRadioButton maleRadioButton;
    private javax.swing.JTextField mobileTextField;
    private javax.swing.JTextField officeTextField;
    private javax.swing.JTextField phoneTextField;
    private javax.swing.JTextArea shipToAddressTextArea;
    private javax.swing.JComboBox shipToCityComboBox;
    private javax.swing.JComboBox shipToStateComboBox;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    
    KeyListener keyListener = new KeyListener() {

        @Override
        public void keyTyped(KeyEvent e) {}

        @Override
        public void keyPressed(KeyEvent e) {}

        @Override
        public void keyReleased(KeyEvent e) {
            if(e.getKeyCode() == KeyEvent.VK_ENTER) { 
                createOrSaveCustomerButtonActionPerformed(null);
            }
        }
    };
}